import Deque from '../data-structures/deque';
/**
 * 用双端队列实现回文检查器
 * @param aString 目标字符串
 */
export function palindromeChecker(aString: string) {

    if (aString === undefined || aString === null ||
        (aString !== null && aString.length === 0)) {
        return false;
    }

    const deque = new Deque<string>();
    const lowerString = aString.toLocaleLowerCase().split(' ').join(''); // 小写化，去除空格
    let firstChar: string;
    let lastChar: string;

    for (let i = 0; i < lowerString.length; i++) {
        deque.addBack(lowerString.charAt(i)); // 依次存入双端队列中
    }

    while (deque.size() > 1) { // 可能剩中间一个字符
        firstChar = deque.removeFront(); // 从首端取一个
        lastChar = deque.removeBack(); // 从尾端取一个
        if (firstChar !== lastChar) {
            return false;
        }
    }

    return true;
}
